﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using SlimDX.Direct3D9;
using SlimDX;

namespace GameStudio.Framework.GraphicsEngine
{
    public abstract class Program
    {
        private Camera camera;
        public Program()
        {
            this.VisualObjets = new List<IVisualObject>();
        }

        protected virtual void OnInitilizing()
        {
            camera = new Camera();
            this.VisualObjets.Add(camera);

        }
        protected virtual void OnInitilized()
        {}

        protected virtual void OnRendering()
        {
            
        }
        protected virtual void OnRendered()
        {}

        private void InitilizeVisualObjects()
        {
            OnInitilizing();
            foreach(var item in this.VisualObjets)
                item.Initilize();
            OnInitilized();
        }

        private void RenderVisualObjects()
        {
            OnRendering();
            foreach(var item in this.VisualObjets)
                item.Render();
            OnRendered();
        }

        public List<IVisualObject> VisualObjets { private set; get; }

        public void Run()
        {
            //DeviceContext.Current.Initilize();
            DeviceContext.Current.MainForm.Show();
            DeviceContext.Current.MainForm.Closed += (o, e) => Application.Exit();
            InitilizeVisualObjects();
            //---------------------------------------------------------
            while (DeviceContext.Current.MainForm.Created)
            {
                
                Application.DoEvents();
                DeviceContext.Current.Device.Clear(ClearFlags.Target, Color.Black, 0, 0);
                DeviceContext.Current.Device.BeginScene();
                
                DeviceContext.Current.Device.SetRenderState(RenderState.Ambient, Color.White.ToArgb());

                RenderVisualObjects();

                DeviceContext.Current.Device.EndScene();
                DeviceContext.Current.Device.Present();
            }
        }
    }
}
